package com.gitee.wsl.compose.ui.section.lazylist.item

import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.spring
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.unit.Dp
import com.gitee.wsl.compose.ui.section.CupertinoSectionDefaults
import com.gitee.wsl.compose.ui.section.lazylist.LazySectionScope

fun LazySectionScope.Picker(
    contentType: Any?,
    expanded : Boolean,
    text : @Composable () -> String,
    onExpandedChange : (Boolean) -> Unit,
    enabled: Boolean = true,
    icon: (@Composable () -> Unit)? = null,
    dividerPadding: Dp = if (icon != null)
        CupertinoSectionDefaults.DividerPaddingWithIcon
    else CupertinoSectionDefaults.DividerPadding,
    button : @Composable (
        buttonModifier : Modifier,
        titleModifier: Modifier,
        text : String
    ) -> Unit = { buttonModifier, titleModifier, text ->
        CupertinoSectionDefaults.PickerButton(
            modifier = buttonModifier,
            expanded = expanded,
            title = {
                Text(
                    text = text,
                    modifier = titleModifier
                )
            }
        )
    },
    content : @Composable () -> Unit,
    title: @Composable () -> Unit,
) = expandableRow(
    key = null,
    contentType = contentType,
    dividerPadding = dividerPadding,
    title = title,
    belowContentExpanded = expanded,
    belowContent = content
) {

    val updatedOnExpandedChange by rememberUpdatedState(onExpandedChange)

    val interactionSource = remember {
        MutableInteractionSource()
    }

    val pressed by interactionSource.collectIsPressedAsState()

    val titleText = text()

    val animatedTextAlpha by animateFloatAsState(
        targetValue = if (pressed) 0.5f else 1f,
        animationSpec = spring(stiffness = Spring.StiffnessLow),
        label = "Section Date picker fade animation"
    )
    button(
        Modifier.clickable(
            interactionSource = interactionSource,
            enabled = enabled,
            indication = null,
            onClick = {
                updatedOnExpandedChange(!expanded)
            }
        ),
        Modifier.graphicsLayer {
            alpha = animatedTextAlpha
        },
        titleText
    )
}